<?php
class Group extends Admin_Controller
{
	
	function __construct()
	{
		parent::__construct();
		$this->load->model('group_model','group');
	}
	
	function index()
	{
		//$this->db->debug = TRUE;
		$where = '1=1';
		if(!empty($_GET['search']))
		{
			$where .= ' and groups.name like \'%'.$_GET['search'].'%\'';
		}
		if(!empty($_GET['division_id']))
		{
			$where .= ' and groups.division_id = '.$_GET['division_id'];
		}
		$this->group->where($where);
		$data['result'] = $this->group->get();
		$data['pagination'] = $this->group->pagination();
		$this->template->build('group_index',$data);
	}
	
	function form($id = NULL)
	{
		$data['rs'] = $this->group->get_row($id);
		$this->template->build('group_form',$data);
	}
	
	function save()
	{
		if($_POST)
		{
			if(empty($_POST['id']))
			{
				add_log('เพิ่มกลุ่ม/ฝ่าย "'.$_POST['name'].'"');
			}
			else
			{
				add_log('แก้ไขกลุ่ม/ฝ่าย "'.$_POST['name'].'"');
			}
			$this->group->save($_POST);
			set_notify('success', lang('save_data_complete'));
		}
		redirect('division/group');
	}
	
	function delete($id)
	{
		$rs = $this->group->get_row($id);
		add_log('ลบกลุ่ม/ฝ่าย "'.$rs['name'].'"');
		$this->group->delete($id);
		set_notify('success', lang('delete_data_complete'));
		redirect($_SERVER['HTTP_REFERER']);
	}
	
	function ajax_ddl($category_id,$id = NULL)
	{
		echo form_dropdown('group_id',get_option('ID','NAME','GROUPS','DIVISION_ID = '.$category_id),$id); 
	}
}
?>